package com.zryc.shop.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.pms.entity.Brand;
import com.zryc.shop.pms.entity.ProductAttribute;
import com.zryc.shop.pms.entity.ProductAttributeCategory;
import com.zryc.shop.pms.service.IBrandService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/pms/brand")
@Api(tags = "品牌管理接口列表")
public class BrandController {
    @Autowired
    private IBrandService brandService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/list")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@RequestParam(required = false) @ApiParam(value = "品牌名称关键词") String keyword,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        //调用我们自己实现的分页方法，这个方法是走Redis缓存
        IPage page = brandService.page(keyword, pageNum, pageSize);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("创建品牌")
    public ResponseResult create(@RequestBody Brand brand) {
        redisTemplate.expire("BRAND_LIST_KEY", 1, TimeUnit.MILLISECONDS);//让该key立刻过期
        brandService.save(brand);
        return new ResponseResult(200, "创建成功");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除品牌")
    public ResponseResult delete(@PathVariable @ApiParam(value = "品牌ID", required = true) Long id) {
        redisTemplate.expire("BRAND_LIST_KEY", 1, TimeUnit.MILLISECONDS);//让该key立刻过期
        brandService.removeById(id);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/showStatus")
    public ResponseResult updateShowStatus(@RequestParam @ApiParam(value = "品牌ID列表") String ids,
                                           @RequestParam @ApiParam(value = "显示状态") Integer showStatus) {
        redisTemplate.expire("BRAND_LIST_KEY", 1, TimeUnit.MILLISECONDS);//让该key立刻过期
        UpdateWrapper<Brand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("show_status", showStatus);
        brandService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/update/factoryStatus")
    public ResponseResult updateFactoryStatus(@RequestParam @ApiParam(value = "品牌ID列表") String ids,
                                              @RequestParam @ApiParam(value = "是否是生产商") Integer factoryStatus) {
        redisTemplate.expire("BRAND_LIST_KEY", 1, TimeUnit.MILLISECONDS);//让该key立刻过期
        UpdateWrapper<Brand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids.split(","));
        updateWrapper.set("factory_status", factoryStatus);
        brandService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询详情")
    public ResponseResult detail(@PathVariable @ApiParam(value = "品牌ID", required = true) Long id) {
        return new ResponseResult(200, "查询完成", brandService.getById(id));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改品牌")
    public ResponseResult detail(@PathVariable @ApiParam(value = "品牌ID", required = true) Long id,
                                 @RequestBody Brand brand) {
        redisTemplate.expire("BRAND_LIST_KEY", 1, TimeUnit.MILLISECONDS);//让该key立刻过期
        brand.setId(id);
        brandService.updateById(brand);
        return new ResponseResult(200, "修改成功");
    }
}
